package com.lesserpand.rabbitmq.common;

import com.lesserpand.rabbitmq.netty.listener.NettyServer;
import org.redisson.Redisson;
import org.redisson.api.RQueue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author bailuo
 * @version 2.0
 * @Description TODO
 * @Date Created in 2019/1/22 17:16
 */
@Component
public class ApplicationCommanLineRunner implements CommandLineRunner {

    private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationCommanLineRunner.class);

    @Autowired
    private Redisson redisson;

    @Resource
    private NettyServer nettyServerListener;

    @Override
    public void run(String... args) throws Exception {
        // netty 启动监听
        nettyServerListener.start();

        // redisson 启动监听
        RQueue<String> distinationQueue = redisson.getQueue("message_queue");
        while (true) {
            while (null != distinationQueue.peek()) {
                String msg = distinationQueue.poll();
                LOGGER.info(msg);
                // 可以处理业务逻辑
            }
            Thread.sleep(1000);
        }
    }

}
